package com.hbean.controller;

import com.hbean.entity.SysDic;
import com.hbean.service.SysDicService;
import com.hbean.system.annotation.LogMethod;
import com.hbean.system.config.Base;
import com.hbean.utils.HUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: null
 * @Project: hbean
 * @Author: hjibo
 * @CreateDate: 2018-09-28 23:46
 * @ModificationHistory:
 */

@Slf4j
@RestController
@RequestMapping("/sysDic")
public class SysDicController extends Base {

    @Autowired
    private SysDicService sysDicService;

    /**
     * description: 查询
     * create_user: hjibo
     * create_date: 2018-09-29 10:13
     * param :
     */
    @RequestMapping("/getSysDics")
    public List<SysDic> getSysDics(HttpSession session){
        List<SysDic> sysDics = null;
        try {
            sysDics = sysDicService.getDataForCache();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sysDics;
    }

    /**
     * description: 查询
     * create_user: hjibo
     * create_date: 2018-09-29 10:13
     * param :
     */
    @RequestMapping("/getSysDic")
    public SysDic getSysDic(HttpServletRequest request){
        SysDic sysDic = null;
        try {
            List<SysDic> sysDics = sysDicService.getDataForCache();//从缓存中取数据
            sysDics = sysDics.stream().filter(sysDic1 -> sysDic1.getSd00().equals(request.getParameter("sd00"))).collect(Collectors.toList());
            sysDic = sysDics.get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sysDic;
    }


    /**
     * description: 新增
     * create_user: hjibo
     * create_date: 2018-09-29 10:19
     * param :
     */
    @RequestMapping("/addSysDic")
    @LogMethod(logName = "新增字典项")
    public Map addSysDic(HttpServletRequest request,HttpSession session){
        try {
            SysDic sysDic = new SysDic();

            if(HUtil.isNotEmpty(request.getParameter("sd01"))) sysDic.setSd01(request.getParameter("sd01"));
            if(HUtil.isNotEmpty(request.getParameter("sd02"))) sysDic.setSd02(request.getParameter("sd02"));
            if(HUtil.isNotEmpty(request.getParameter("sd04"))) sysDic.setSd04(request.getParameter("sd04"));
            if(HUtil.isNotEmpty(request.getParameter("sd05"))) sysDic.setSd05(request.getParameter("sd05"));
            if(HUtil.isNotEmpty(request.getParameter("sd10"))) sysDic.setSd10(Integer.parseInt(request.getParameter("sd10")));

            this.sysDicService.insertDataAndUpdateCache(sysDic);

            return mapResult(SUCCESS_CODE,SUCCESS_MSG,LONG_0L,null);
        } catch (Exception e) {
            log.info("保存字典信息失败！");
            e.printStackTrace();
        }
        return mapResult(ERROR_CODE,ERROR_MSG,LONG_0L,null);
    }

    /**
     * description: 删除
     * create_user: hjibo
     * create_date: 2018-09-29 15:50
     * param :
     */
    @RequestMapping("/removeSysDic")
    @LogMethod(logName = "删除字典项")
    public Map removeSysDic(HttpServletRequest request){
        try {
            SysDic sysDic = new SysDic();
            if(HUtil.isNotEmpty(request.getParameter("sd00"))){
                sysDic.setSd00(request.getParameter("sd00"));
                this.sysDicService.deleteDataAndUpdateCache(sysDic);
                return mapResult(SUCCESS_CODE,SUCCESS_MSG,LONG_0L,null);
            }
        } catch (Exception e) {
            log.info("删除字典信息失败！");
            e.printStackTrace();
        }
        return mapResult(ERROR_CODE,ERROR_MSG,LONG_0L,null);
    }

    /**
     * description: 修改
     * create_user: hjibo
     * create_date: 2018-09-29 10:19
     * param :
     */
    @RequestMapping("/modSysDic")
    @LogMethod(logName = "修改字典项")
    public Map modSysDic(HttpServletRequest request){

        try {
            SysDic sysDic = new SysDic();

            if(HUtil.isNotEmpty(request.getParameter("sd00"))) sysDic.setSd00(request.getParameter("sd00"));
            if(HUtil.isNotEmpty(request.getParameter("sd01"))) sysDic.setSd01(request.getParameter("sd01"));
            if(HUtil.isNotEmpty(request.getParameter("sd05"))) sysDic.setSd05(request.getParameter("sd05"));
            if(HUtil.isNotEmpty(request.getParameter("sd10"))) sysDic.setSd10(Integer.parseInt(request.getParameter("sd10")));

            this.sysDicService.updateDataAndUpdateCache(sysDic);
            return mapResult(SUCCESS_CODE,SUCCESS_MSG,LONG_0L,null);
        } catch (Exception e) {
            log.info("修改字典信息失败！");
            e.printStackTrace();
        }
        return mapResult(ERROR_CODE,ERROR_MSG,LONG_0L,null);
    }



}
